import { post, get } from "../../../utils/request";
import utils from "../../../utils/utils";

const env = import.meta.env;
const contextPath = env.VITE_API_AUTH_CONTEXT_PATH;
const url = env.VITE_API_URL;

const apiUser = {
  generateAvatarUrl(avatarUrl: string) {
    // 其他方法是 post，在 request.ts 中指定了 env.VITE_API_URL，当前方法仅返回一个 url，所以需要单独配置
    return (
      url +
      contextPath +
      avatarUrl +
      "?x-summer-token=" +
      window.store.tokenInfo.token +
      "&t=" +
      utils.getUUID()
    );
  },

  changeAvatar(formData: FormData) {
    return post(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/user/api/change.avatar",
      formData
    );
  },

  changeAlias(alias: string) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/user/api/change.alias", {
      alias,
    });
  },

  changeGender(gender: number) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/user/api/change.gender", {
      gender,
    });
  },

  changeIdNumber(idNumber: string) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/user/api/change.idNumber", {
      idNumber,
    });
  },

  changeBirthday(birthday: string) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/user/api/change.birthday", {
      birthday,
    });
  },

  changeEmail(email: string) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/user/api/change.email", {
      email,
    });
  },

  changeMobile(mobile: string) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/user/api/change.mobile", {
      mobile,
    });
  },

  changeAccountPassword(
    sessionId: string,
    encryptedOldPassword: string,
    encryptedNewPassword: string
  ) {
    return post(env.VITE_API_ADMIN_CONTEXT_PATH + "/user/api/change.password", {
      sessionId,
      encryptedOldPassword,
      encryptedNewPassword,
    });
  },

  lastLoginTime(accountId: string) {
    return get(env.VITE_API_ADMIN_CONTEXT_PATH + "/user/api/last.login.time", {
      accountId,
    });
  },

  lastChangePasswordTime(accountId: string) {
    return get(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/user/api/last.change.password.time",
      {
        accountId,
      }
    );
  },

  checkPasswordExpiry(accountId: string) {
    return get(
      env.VITE_API_ADMIN_CONTEXT_PATH + "/user/api/check.password.expiry",
      {
        accountId,
      }
    );
  },
};

export default apiUser;
